/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf;

import com.controllers.CompanyDenominationController;
import com.controllers.CreditController;
import com.entities.CompanyDenomination;
import com.entities.Credit;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

/**
 *
 * @author ben
 */
@Named(value = "creditAction")
@RequestScoped
public class CreditAction implements Serializable {

    /**
     * Creates a new instance of CreditAction
     */
    private static final Logger logger = Logger.getLogger(CompanyDenominationController.class.getName());
    private CompanyDenomination companyDenomination;
    private Long companyDenominationID;
    /* EJB required to find the company_denomination id */
    @EJB
    private CompanyDenominationController companyDenominationController;
    /* EJB required to save the actual credit */
    @EJB
    private CreditController creditController;
    @ManagedProperty(value = "creditView")
    CreditView creditView;
    
    public CompanyDenomination getCompanyDenomination() {
        return companyDenomination;
    }
    
    public void setCompanyDenomination(CompanyDenomination companyDenomination) {
        this.companyDenomination = companyDenomination;
    }
    
    public Long getCompanyDenominationID() {
        return companyDenominationID;
    }
    
    public void setCompanyDenominationID(Long companyDenominationID) {
        this.companyDenominationID = companyDenominationID;
    }
    
    public CreditAction() {
        companyDenomination = new CompanyDenomination();
        credit = new Credit();
    }
    private Credit credit;
    
    public Credit getCredit() {
        return credit;
    }
    
    public void setCredit(Credit credit) {
        this.credit = credit;
    }
    
    public String save() {
        // First, find the company_denomination entity
        try {
            CompanyDenomination compDenom = companyDenominationController.findById(companyDenominationID);
            credit.setCompanyDenomination(compDenom);
            /* set autogenerated fields */
            
            Long creditId = creditController.saveCredit(credit);
            logger.log(Level.INFO, "Saving Credit Successful: {0}", creditId);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saving Credit Successful", "Saving Credit Successful"));
            //creditView.creditTab(3);
            setCredit(new Credit());
            return null;
            
            
        } catch (Exception e) {
            logger.warning("Unable to save a credit");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unable to create Credit", "Unable to create Credit"));
            //creditView.creditTab(3);
            return "index";
        }
        
    }
    
    public CompanyDenominationController getCompanyDenominationController() {
        return companyDenominationController;
    }
    
    public void setCompanyDenominationController(CompanyDenominationController companyDenominationController) {
        this.companyDenominationController = companyDenominationController;
    }
    
    public CreditController getCreditController() {
        return creditController;
    }
    
    public void setCreditController(CreditController creditController) {
        this.creditController = creditController;
    }
    
    public CreditView getCreditView() {
        return creditView;
    }
    
    public void setCreditView(CreditView creditView) {
        this.creditView = creditView;
    }
}
